package com.ddd.controller;


import com.ddd.common.CommonResponse;
import com.ddd.constants.HttpResponseCode;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.HttpStatus;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: liuqisong
 * @Date: 2021/4/17 11:41
 * @Description:
 */
@RestController
public class CommonErrorController implements ErrorController {

    private static String errorPath = "/error";

    private static String error = "请求繁忙...请稍等";

    private static String exceptionPath = "javax.servlet.error.exception";

    @Override
    public String getErrorPath() {
        return errorPath;
    }

    @RequestMapping
    public CommonResponse<Object> reError(HttpServletRequest request, HttpServletResponse response) {

        response.setStatus(HttpStatus.SC_OK);

        String message = error;
        Exception exception = (Exception) request.getAttribute(exceptionPath);

        if (exception instanceof ZuulException) {
            message = exception.getMessage();
        }

        return new CommonResponse<>(HttpResponseCode.ERROR.getVal(), message);
    }
}
